package com.bonc.rms.service.impl.tms;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.dao.tms.IEntryLeaveDao;
import com.bonc.rms.service.tms.IEntryLeaveService;
import com.bonc.rms.utils.CalendarUtil;

@Service
public class EntryLeaveServiceImpl implements IEntryLeaveService {

	@Autowired
	IEntryLeaveDao entryLeaveDao;
	
	@Override
	public Map<String, Object> getEntryLeaveData(Integer departmentId, String startMonth, String endMonth) {
		LogManager.Method("In  Service : EntryLeaveServiceImpl -> getEntryLeaveData");
		
		List<String> entryNum = getEntryNum(departmentId, startMonth, endMonth);
		Map<String, Object> entryNumMap = new HashMap<String, Object>();
		entryNumMap.put("name", "入职人数");
		entryNumMap.put("type", "bar");
		entryNumMap.put("data", entryNum.toArray());
		
		List<String> leaveNum = getLeaveNum(departmentId, startMonth, endMonth);
		Map<String, Object> leaveNumMap = new HashMap<String, Object>();
		leaveNumMap.put("name", "离职人数");
		leaveNumMap.put("type", "bar");
		leaveNumMap.put("data", leaveNum.toArray());
		
		List<String> entryRate = getEntryRate(departmentId, startMonth, endMonth);
		Map<String, Object> entryRateMap = new HashMap<String, Object>();
		entryRateMap.put("name", "入职率");
		entryRateMap.put("type", "line");
		entryRateMap.put("yAxisIndex", "1");
		entryRateMap.put("data", entryRate.toArray());
		
		List<String> leaveRate = getLeaveRate(departmentId, startMonth, endMonth);
		Map<String, Object> leaveRateMap = new HashMap<String, Object>();
		leaveRateMap.put("name", "离职率");
		leaveRateMap.put("type", "line");
		leaveRateMap.put("yAxisIndex", "1");
		leaveRateMap.put("data", leaveRate.toArray());
		
		List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
		dataList.add(entryRateMap);
		dataList.add(leaveRateMap);
		dataList.add(entryNumMap);
		dataList.add(leaveNumMap);
		
		
		Map<String, Object> entryLeaveMap = new HashMap<String, Object>();
		List<String> titleList = new ArrayList<String>();
		titleList.add("入职率");
		titleList.add("离职率");
		titleList.add("入职人数");
		titleList.add("离职人数");
		
		entryLeaveMap.put("title", titleList);
		entryLeaveMap.put("name1", "人数");
		entryLeaveMap.put("name2", "比率");
		entryLeaveMap.put("dates", CalendarUtil.getMonthList(startMonth, endMonth));
		entryLeaveMap.put("data", dataList);
		
		LogManager.Method("Out Service : EntryLeaveServiceImpl -> getEntryLeaveData");
		
		return entryLeaveMap;
	}
	
	private Map<String, Integer> getEntryMap(Integer departmentId, String startMonth, String endMonth) {
		List<Map<String, Object>> mapList = entryLeaveDao.getEntryMap(departmentId, startMonth, endMonth);
		Map<String, Integer> retMap = new HashMap<String, Integer>();
		for(Map<String, Object> map : mapList) {
			retMap.put((String) map.get("MONTH"), ((Long) map.get("SUM")).intValue());
		}
		return retMap;
	}

	private Map<String, Integer> getLeaveMap(Integer departmentId, String startMonth, String endMonth) {
		List<Map<String, Object>> mapList = entryLeaveDao.getLeaveMap(departmentId, startMonth, endMonth);
		Map<String, Integer> retMap = new HashMap<String, Integer>();
		for(Map<String, Object> map : mapList) {
			retMap.put((String) map.get("MONTH"), ((Long) map.get("SUM")).intValue());
		}
		return retMap;
	}

	private int getInitialNumber(Integer departmentId, String endMonth) {
		return entryLeaveDao.getInitialNumber(departmentId, endMonth);
	}
	
	/**
	 * 获取离职数
	 */
	private List<String> getLeaveNum(Integer departmentId, String startMonth, String endMonth) {
		
		LogManager.Method("In  Service : EntryLeaveServiceImpl -> getLeaveNum");
		
		List<String> leaveNum = new ArrayList<String>();

		Map<String, Integer> retMap = getLeaveMap(departmentId, startMonth, endMonth);
		
		List<String> monthList = CalendarUtil.getMonthList(startMonth, endMonth);
		
		for(int i = 0; i < monthList.size(); i ++) {
			if(!retMap.containsKey(monthList.get(i))) {
				retMap.put(monthList.get(i), 0);
			}
			leaveNum.add(String.valueOf(retMap.get(monthList.get(i))));
		}
		
		LogManager.Method("Out Service : EntryLeaveServiceImpl -> getLeaveNum");
		
		return leaveNum;
	}
	
	/**
	 * 获取入职数
	 */
	private List<String> getEntryNum(Integer departmentId, String startMonth, String endMonth) {
		
		LogManager.Method("In  Service : EntryLeaveServiceImpl -> getEntryNum");
		
		List<String> entryNum = new ArrayList<String>();

		Map<String, Integer> retMap = getEntryMap(departmentId, startMonth, endMonth);
		
		List<String> monthList = CalendarUtil.getMonthList(startMonth, endMonth);
		
		for(int i = 0; i < monthList.size(); i ++) {
			if(!retMap.containsKey(monthList.get(i))) {
				retMap.put(monthList.get(i), 0);
			}
			entryNum.add(String.valueOf(retMap.get(monthList.get(i))));
		}
		
		LogManager.Method("Out Service : EntryLeaveServiceImpl -> getEntryNum");
		
		return entryNum;
	}
	
	/**
	 * 获取离职率
	 */
	private List<String> getLeaveRate(Integer departmentId, String startMonth, String endMonth) {
		
		LogManager.Method("In  Service : EntryLeaveServiceImpl -> getLeaveRate");
		
		List<String> leaveRate = new ArrayList<String>();
		
		Map<String, Integer> entryMap = getEntryMap(departmentId, startMonth, endMonth);
		Map<String, Integer> leaveMap = getLeaveMap(departmentId, startMonth, endMonth);
		
		List<String> monthList = CalendarUtil.getMonthList(startMonth, endMonth);
		
		int startSum = getInitialNumber(departmentId, startMonth);
		double rate = 0.00;
		int curDateLeaveNum;
		int curDateEntryNum;
		DecimalFormat df =(DecimalFormat) NumberFormat.getInstance();
		df.setMaximumFractionDigits(2);
		
		for(int i = 0; i < monthList.size(); i ++) {
			if(!entryMap.containsKey(monthList.get(i))) {
				entryMap.put(monthList.get(i), 0);
			}
			if(!leaveMap.containsKey(monthList.get(i))) {
				leaveMap.put(monthList.get(i), 0);
			}
			curDateLeaveNum = leaveMap.get(monthList.get(i));
			curDateEntryNum = entryMap.get(monthList.get(i));
			startSum = startSum + curDateEntryNum;
			rate = curDateLeaveNum * 1.0 / (startSum == 0 ? Integer.MAX_VALUE : startSum);
			leaveRate.add(df.format(rate * 100));
		}
		
		LogManager.Method("Out Service : EntryLeaveServiceImpl -> getLeaveRate");
		
		return leaveRate;
	}
	
	/**
	 * 获取入职率
	 */
	private List<String> getEntryRate(Integer departmentId, String startMonth, String endMonth) {
		
		LogManager.Method("In  Service : EntryLeaveServiceImpl -> getEntryRate");
		
		List<String> entryRate = new ArrayList<String>();

		Map<String, Integer> retMap = getEntryMap(departmentId, startMonth, endMonth);
		
		List<String> monthList = CalendarUtil.getMonthList(startMonth, endMonth);
		
		int startSum = getInitialNumber(departmentId, startMonth);
		double rate = 0.00;
		int curDateNum;
		DecimalFormat df =(DecimalFormat) NumberFormat.getInstance();
		df.setMaximumFractionDigits(2);
		
		for(int i = 0; i < monthList.size(); i ++) {
			// 填充数据库中没有的月份
			if(!retMap.containsKey(monthList.get(i))) {
				retMap.put(monthList.get(i), 0);
			}
			curDateNum = retMap.get(monthList.get(i));
			startSum = startSum + curDateNum;
			rate = curDateNum * 1.0 / (startSum == 0 ? Integer.MAX_VALUE : startSum);
			entryRate.add(df.format(rate * 100));
		}
		
		LogManager.Method("Out Service : EntryLeaveServiceImpl -> getEntryRate");
		
		return entryRate;
	}

}
